/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * AdminBookingDialog.java
 *
 * Created on May 17, 2012, 11:43:30 AM
 */
package GUI.Admin;

import dao.ChoNgoiDAO;
import java.awt.BorderLayout;
import java.util.List;
import javax.swing.JPanel;
import main.MainFormApp;
import pojo.Chongoi;
import pojo.Lichchieuphim;
import pojo.Phongchieu;
import utils.ClassUtils;

/**
 *
 * @author Hnmtuan
 */
public class AdminBookingDialog extends javax.swing.JDialog {
    // <editor-fold defaultstate="collapsed" desc="Define all variables using this application">

    private Lichchieuphim _ownedSchedule;
    public static final int DEFAULT_WIDTH = 800;
    public static final int DEFAUL_HEIGHT = 600;
    //</editor-fold>
    //<editor-fold defaultstate="collapsed" desc="Define messages and error messages" >
    // </editor-fold>
    // <editor-fold defaultstate="collapsed" desc="Define Constructor" >

    public AdminBookingDialog(java.awt.Frame parent, boolean modal, Lichchieuphim paramSchedule) throws Exception {
        super(parent, modal);
        _ownedSchedule = paramSchedule;
        initComponents();
        initActions();
        // Load Mapping button
        loadButtonsGUI();
    }
    // </editor-fold>
    // <editor-fold defaultstate="collapsed" desc="Define Init Actions" >

    protected final void initActions() {
        jpnBookings.setLayout(new BorderLayout());
        jbtnRefresh.addActionListener(new java.awt.event.ActionListener() {

            public void actionPerformed(java.awt.event.ActionEvent evt) {
                try {
                    clearContentButton();
                    loadButtonsGUI();
                } catch (Exception e) {
                }
            }
        });
    }
    //</editor-fold>
    // <editor-fold defaultstate="collapsed" desc="Implement All Actions">
    // </editor-fold>
    // <editor-fold defaultstate="collapsed" desc="Implement All Property or State">
    // </editor-fold>
    // <editor-fold defaultstate="collapsed" desc="Implement Actions to GUI" >

    protected final void clearContentButton() {
        jpnBookings.removeAll();
    }

    protected final void loadButtonsGUI() throws Exception {
        // Check if ownSchedule empty
        if (_ownedSchedule == null) {
            clearContentButton();
            return;
        }
        try {
            Lichchieuphim cachedSchedule = _ownedSchedule;
            // Get room
            Phongchieu cachedRoom = cachedSchedule.getPhongchieu();
            // Get places
            List<Chongoi> cachedPlaces = ChoNgoiDAO.getChoNgoiByPhongChieuExt(cachedRoom);
            // Generate button layouts
            JPanel generatedPanel = ClassUtils.generateBookingButtonUI(cachedRoom, cachedPlaces, cachedSchedule);
            // add component into 
            jpnBookings.add(generatedPanel, BorderLayout.CENTER);
            MainFormApp.getApplication().getMainFrame().pack();
        } catch (Exception e) {
            throw new Exception(e.getMessage());
        } finally {
            ChoNgoiDAO.closeCurrentSession();
        }
    }
    // </editor-fold>

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jpnBookings = new javax.swing.JPanel();
        jPanel2 = new javax.swing.JPanel();
        jbtnRefresh = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance().getContext().getResourceMap(AdminBookingDialog.class);
        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(resourceMap.getString("jpPlace.border.title"))); // NOI18N
        jPanel1.setName("jPanel1"); // NOI18N

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        jpnBookings.setName("jpnBookings"); // NOI18N

        javax.swing.GroupLayout jpnBookingsLayout = new javax.swing.GroupLayout(jpnBookings);
        jpnBookings.setLayout(jpnBookingsLayout);
        jpnBookingsLayout.setHorizontalGroup(
            jpnBookingsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 473, Short.MAX_VALUE)
        );
        jpnBookingsLayout.setVerticalGroup(
            jpnBookingsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 278, Short.MAX_VALUE)
        );

        jScrollPane1.setViewportView(jpnBookings);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 475, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 280, Short.MAX_VALUE)
                .addContainerGap())
        );

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder(resourceMap.getString("jpnActions.border.title"))); // NOI18N
        jPanel2.setName("jPanel2"); // NOI18N

        jbtnRefresh.setText(resourceMap.getString("jbtnRefresh.text")); // NOI18N
        jbtnRefresh.setName("jbtnRefresh"); // NOI18N

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGap(103, 103, 103)
                .addComponent(jbtnRefresh)
                .addContainerGap(321, Short.MAX_VALUE))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jbtnRefresh)
                .addContainerGap(33, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jPanel1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JButton jbtnRefresh;
    private javax.swing.JPanel jpnBookings;
    // End of variables declaration//GEN-END:variables
}
